package com.tcm.pay.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.tcm.consultation.dto.ConsultationDTO;
import com.tcm.consultation.service.UserConsultationService;
import com.tcm.pay.domain.dto.PaymentDTO;
import com.tcm.pay.enums.BusinessCodeEnum;
import com.tcm.pay.enums.TradeTypeEnum;
import com.tcm.pay.service.OrderPayService;
import com.tcm.pay.service.PaymentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

@DubboService(version = "0.0.1-alpha", group="consultation", delay = -1, timeout = 6000)
public class ConsultationPayServiceImpl implements OrderPayService {

    @DubboReference(version = "0.0.1-alpha", check = false, timeout = 3000)
    UserConsultationService consultationService;

    @Autowired
    PaymentService paymentService;

    @Autowired
    PayService payService;

    private static final String aliAdviceUrl = "http://api.zetikeji.com/api/pay/consultation/ali/notify";

    private static final String wxAdviceUrl = "http://api.zetikeji.com/api/pay/consultation/wx/notify";

    @Override
    public Map<String, Object> alipay(Long userId, Long orderId) throws ServiceException {
        try {
            initPayment(userId, orderId);
            payService.setAliAdviceUrl(aliAdviceUrl);
            return payService.alipay(userId, orderId, TradeTypeEnum.CONSULTATION.getCode());
        } catch (ServiceException e) {
            e.printStackTrace();
            R.throwOnFail(e.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            R.throwFail(e.getMessage());
        }
        return null;
    }

    @Override
    public Map<String, Object> wxpay(Long userId, Long orderId, String code) throws ServiceException {
        try {
            initPayment(userId, orderId);
            payService.setWxAdviceUrl(wxAdviceUrl);
            return payService.wxpay(userId, orderId, code, TradeTypeEnum.CONSULTATION.getCode());
        } catch (Exception e) {
            e.printStackTrace();
            R.throwFail(e.getMessage());
        }
        return null;
    }

    private void initPayment(Long userId, Long orderId) throws ServiceException {
        ConsultationDTO consultationDTO;
        try {
            consultationDTO = consultationService.findConsultationById(orderId);
            if(ObjectUtil.isEmpty(consultationDTO)){
                R.throwFail(BusinessCodeEnum.ORDER_NOT_EXISTS);
            }
            if(!consultationDTO.getStatus().equals(new Byte("0"))){
                R.throwFail(BusinessCodeEnum.ORDER_PAY_NOT_EXISTS);
            }
            //后期优化，需要先查询第三方支付状态来判断是否重新支付
            PaymentDTO paymentDTO = paymentService.findByOrderId(userId,orderId, TradeTypeEnum.CONSULTATION.getCode());
            if(ObjectUtil.isEmpty(paymentDTO)){
                paymentDTO = new PaymentDTO();
                //初始化支付单
                paymentDTO.setDataId(orderId);
                paymentDTO.setTargetId(userId);
                paymentDTO.setStatus(new Byte("0"));
                paymentDTO.setDirection(new Byte("1"));
                paymentDTO.setSubject("问诊单");
                paymentDTO.setDescription("问诊咨询费支付");
                paymentDTO.setOuterTradeNo(consultationDTO.getOrderSn());
                paymentDTO.setPayChannel(consultationDTO.getPayChannel());
                paymentDTO.setDataSrc(TradeTypeEnum.CONSULTATION.getCode());
                paymentDTO.setPayAmount(consultationDTO.getPrice());
                paymentService.create(paymentDTO);
            }else{
                //为了能重复支付订单，而做此处理
                paymentService.createNewPaySn(orderId, userId, TradeTypeEnum.CONSULTATION.getCode());
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            R.throwOnFail(e.getResult());
        } catch (Exception e) {
            e.printStackTrace();
            R.throwFail(e.getMessage());
        }
    }

}
